//
//  LSLeagueFixturesViewController.m
//  LookScore
//
//  Created by warrior on 2018/6/21.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLeagueFixturesViewController.h"
#import "UIViewController+ZJScrollPageController.h"
#import "LSPulldDownOptionsView.h"//下拉view
#import "LSLeagueFixtruesModel.h"
#import "LSLeagueDetailCell.h"

@interface LSLeagueFixturesViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)LSPulldDownOptionsView *pullView;

@property(nonatomic,retain)NSMutableArray *dataArray;

@property(strong,nonatomic)UIButton *categoryBu;

//@property(nonatomic,assign) NSInteger leagueType;

//@property(assign,nonatomic)NSInteger  pageIndex;

@end

@implementation LSLeagueFixturesViewController
//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:YES];
//
//}

- (instancetype)init{
    self = [super init];
    if (self) {
        [notifiCenter addObserver:self selector:@selector(getRound:) name:@"GETROUND" object:nil];
    }
    return self;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}
- (void)setUpdate:(NSString *)update{
    self.season = update;
    [self loadLeagueFixturesData:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}

-(void)zj_viewDidLoadForIndex:(NSInteger)index{
    
    
    
//    self.leagueType = 1;
//    if (self.leagueType == 1) {
        self.tableView.tableHeaderView = [self tableHeadView];
//    }else{
//
//    }
    [self.view addSubview:self.tableView];
    
    
    LSRefreshHeaderView *header = [LSRefreshHeaderView headerWithRefreshingBlock:^{
        [self loadLeagueFixturesData:YES];
    }];
    self.tableView.mj_header = header;// 设置header
    [header beginRefreshing];
    
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        [self loadLeagueFixturesData:NO];
//    }];
}


- (void)getRound:(NSNotification *)text{

    MyLog(@"%@",text.userInfo);
    self.rounding = text.userInfo[@"Rounding"];
    self.totalRound = text.userInfo[@"TotalRound"];
    MyLog(@"－－－－－接收到通知------");

}


- (void)loadLeagueFixturesData:(BOOL)refresh{
    //    if (refresh) {
    //
    //        self.pageIndex = 1;
    //    }else{
    //
    //        self.pageIndex += 1;
    //    }
    MyLog(@"leagueId --- %@  season --- %@  round --- %@",self.leagueId,self.season,self.rounding);
    [LSHttpEnclosureTool GET:LSLeagueMatch parameters:@{@"leagueId":self.leagueId,@"matchSeason":self.season,@"round":self.rounding} resBlock:^(id responseObject) {
        if (responseObject) {
            if (refresh) {
                [self.dataArray removeAllObjects];
            }
            MyLog(@"leagueFixtures ---- %@",responseObject);
//              self.dataArray = responseObject;
            
            for (NSDictionary *dic in responseObject) {
                LSLeagueFixtruesModel *model = [LSLeagueFixtruesModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }

        }
        //        else{
        //            self.pageIndex -= 1;
        //
        //        }
        [self endRefreshing];
        [self.tableView reloadData];
    }];
}

- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }
    
}
#pragma mark 🔥 Event

-(void)chooseCategoryEvent:(UIButton *)sender{
    
    NSArray * array = @[@"第1轮",@"第2轮",@"第3轮",@"第4轮",@"第5轮",@"第6轮",@"第7轮",@"第8轮",@"第9轮"];
    
    CGFloat height = 35*array.count;
    if (!self.pullView) {
        LSPulldDownOptionsView *pullView=[[LSPulldDownOptionsView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, height) normalTitleColor:LSNavMainColor selectedTitleColor:LSTitleWhiteColor normalBackViewColor:LSTitleWhiteColor selectedBackViewColor:LSNavMainColor height:height];
        
        pullView.modelArray=array.mutableCopy;
        pullView.cellClickBlock = ^(NSInteger index) {
            [self.categoryBu setTitle:array[index] forState:UIControlStateNormal];
            self.rounding = [NSString stringWithFormat:@"%ld",(long)index];
            [self loadLeagueFixturesData:YES];
        };
        self.pullView=pullView;
        [self.view addSubview:pullView];
    }
    self.pullView.hidden=NO;
    
}

#pragma mark 🔥 tableViewDelegate tableViewDataSource
-(UIView *)tableHeadView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 32)];
    view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *categoryBu=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 32)];
    [categoryBu setTitleColor:LSTitleGrayColor1 forState:UIControlStateNormal];
    [categoryBu setTitle:[NSString stringWithFormat:@"第%@轮",self.rounding] forState:UIControlStateNormal];
    [categoryBu setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    categoryBu.titleLabel.font = LSFontSize12;
    [view addSubview:categoryBu];
    //    navigetionBu.titleEdgeInsets=UIEdgeInsetsMake(0,-50, 0, 50);
    //    navigetionBu.imageEdgeInsets=UIEdgeInsetsMake(0, 60, 0, -60);
    [categoryBu addTarget:self action:@selector(chooseCategoryEvent:) forControlEvents:UIControlEventTouchUpInside];
    self.categoryBu = categoryBu;
    
    return view;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 10)];
    headerView.backgroundColor = LSBackGroundColor;
    
    
    return headerView;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LSLeagueFixtruesCell *cell=[LSLeagueFixtruesCell FixtruesCellByTableView:tableView];
    LSLeagueFixtruesModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    if (indexPath.row % 2 != 0) {
        cell.backgroundColor = LSSubGrayColorf7;
    }
    else{
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}
#pragma mark 🔥 Getter
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, LSScreenHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = 0;
        _tableView.sectionFooterHeight = 0;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end


